<template>
  <j-select-user-by-dep v-model="newValue" :disabled="disabled" :multi="record.options.multiple"></j-select-user-by-dep>
</template>
<script>
import { mapGetters } from 'vuex'
import JSelectUserByDep from '@/components/jeecgbiz/JSelectUserByDep'
export default {
  name: 'User',
  props: ['record', 'value', 'disabled', 'formConfig'],
  components: {
    JSelectUserByDep
  },
  data() {
    return {
      newValue: ''
    }
  },
  watch: {
    value(newValue) { // 用户值改变时   获取用户真实姓名
      this.newValue = newValue
    },
    'record.options.defaultValue'(newValue) {
      if (newValue === '${username}' || newValue === '${realname}') {
        this.$emit('change', this.userInfo().username)
      } else {
        this.$emit('change', newValue)
      }
    },
    'record.options.isCc'(newValue, oldValue) {
      this.setCcList()
    },
    'record.model'(newValue, oldValue) {
      const cclist = this.formConfig.cclist
      cclist && cclist.splice(cclist.indexOf(oldValue), 1)
      this.setCcList()
    },
    newValue(value) {
      this.$emit('change', value)
    }
  },
  methods: {
    ...mapGetters(['userInfo']),
    setCcList() {
      const key = this.record.model
      if (!key) {
        return false
      }
      const value = this.record.options.isCc
      let cclist = this.formConfig.cclist
      if (value) {
        if (!cclist) {
          cclist = []
        }
        cclist.push(key)
      } else {
        cclist && cclist.splice(cclist.indexOf(key), 1)
      }
    }
  },
  mounted() {
    const defaultValue = this.record.options.defaultValue
    if (this.value && this.value !== defaultValue) {
      this.newValue = this.value
    } else {
      if (defaultValue === '${username}' || defaultValue === '${realname}') {
        this.$emit('change', this.userInfo().username)
      }
    }
  }
}
</script>
